﻿using System.Collections;
using System.Collections.Generic;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Rendering;
using Unity.Transforms;
using UnityEngine;

public class Demo01 : MonoBehaviour
{

    [Header("Mesh Info")]
    public Mesh _blockMesh;

    [Header("Material Block")]
    public Material _blockMaterial;

    public GameObject Prefab_ref;

    private static EntityArchetype _BlockArchetype;
    private static EntityManager _EntityManager;

    [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
    private static void Init()
    {
        _EntityManager = World.Active.GetOrCreateManager<EntityManager>();
        _BlockArchetype = _EntityManager.CreateArchetype(typeof(Position));
    }

    private void Start()
    {
        EntityManager manager = World.Active.GetOrCreateManager<EntityManager>();
        Entity entity = manager.CreateEntity(_BlockArchetype);
        manager.SetComponentData(entity, new Position() { Value = new float3(2, 0, 0) });
        manager.AddSharedComponentData(entity, new MeshInstanceRenderer()
        {
            mesh = _blockMesh,
            material = _blockMaterial
        });

        if (Prefab_ref)
        {
            NativeArray<Entity> entityArray = new NativeArray<Entity>(1, Allocator.Temp);
            _EntityManager.Instantiate(Prefab_ref, entityArray);
            _EntityManager.SetComponentData(entityArray[0], new Position() { Value = new float3(4, 0, 0) });
            entityArray.Dispose();
        }
    }
}
